<template>
	<view>
		<u-navbar
			:title="recomandInfo.title"
			leftText="返回"
			:placeholder="true"
			:safeAreaInsetTop="true"
			bgColor="#f8f8f8"
			leftIconSize="42rpx"
			@leftClick="leftClick"
		>
			<view class="u-flex u-nav-slot" slot="left">
				<template v-if="level==2">
					<u-text text="返回" size="28" color="#444" iconStyle="font-size:36rpx;margin-right:8rpx" prefix-icon="arrow-left"></u-text>
				</template>
				
				<template v-if="level==1">
					<u-text text="主页" size="28" color="#444" iconStyle="font-size:36rpx;margin-right:8rpx" prefix-icon="home"></u-text>
				</template>
			</view>
		</u-navbar>
		
		<view class="content-padding white-bg">
			<ftx-routes :routesList="routesList"></ftx-routes>
		</view>
	</view>
</template>

<script>
	import { recomandDetails,recommandRouters } from '@/api/api.js'
	export default {
		data() {
			return {
				recomandInfo: {
					id: 0,
					title: '推荐线路'
				},
				filter: {
					recomand_id: 0,
					page: 1
				},
				routesList: [],
				loadStatus: 'loadmore',
				level: 1,
				spread_uid: uni.getStorageSync('ftxUserId') || 0
			};
		},
		methods: {
			getRoutesList: function() {
				if ( this.loadStatus == 'nomore' ) {
					return console.log('nomore')
				}
				uni.showLoading({
					title: '加载中'
				})
				this.loadStatus = 'loading'
				recommandRouters(uni.$lv.object.queryParams(this.filter)).then(res => {
					uni.hideLoading()
					let _result = res.data || []
					if ( !_result.length ) {
						return this.loadStatus = 'nomore'
					}
					this.routesList = uni.$lv.array.union(this.routesList , _result)
					this.loadStatus = 'loadmore'
				})
			},
			leftClick() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			}
		},
		// 分享给朋友
		onShareAppMessage: function () {
		    const promise = new Promise(resolve => {
				resolve({
				  title: this.recomandInfo.title,
				  path: `/pages/routes/recomand?id=${this.recomandInfo.id}&spread_uid=${this.spread_uid}`
				})
			})
			return {
			  title: this.recomandInfo.title,
			  path: `/pages/routes/recomand?id=${this.recomandInfo.id}&spread_uid=${this.spread_uid}`,
			  promise 
			}
		},
		// 分享到朋友圈
		onShareTimeline: function () {
			return {
			  title: this.recomandInfo.title,
			}
		},
		onLoad(options) {
			this.recomandInfo.id = options.id || 0
			if ( this.recomandInfo.id > 0 ) {
				this.filter.recomand_id = this.recomandInfo.id
				recomandDetails(this.recomandInfo.id).then(res => {
					this.recomandInfo.title = res.data.title || '推荐线路'
					
					this.getRoutesList()
				})
			}
			
			this.level = options.level || 1
			
			let sales_uid = options.sales_uid || 0
			if ( sales_uid > 0 ) {
				uni.setStorageSync('ftxSpreaderSalesUid' , sales_uid)
			}
			
			let spread_uid = options.spread_uid || 0
			if ( spread_uid > 0 ) {
				uni.setStorageSync('ftxSpreaderUid' , spread_uid)
			}
		},
		mounted() {
			
		},
		/**
		 * 下拉刷新回调函数
		 */
		onPullDownRefresh() {
			this.loadStatus = 'loadmore'
			this.filter.page = 1
			this.routesList = []
			this.getRoutesList()
			uni.stopPullDownRefresh()
		},
		/**
		 * 上拉加载回调函数
		 */
		onReachBottom() {
			this.filter.page++
			this.getRoutesList()
		},
	}
</script>

<style lang="scss">

</style>
